package ar.edu.uba.fi.jmit.monitorengine.core.rangeobserver;

import java.io.Serializable;
import java.util.Map;

import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.integrationcommons.EventPriority;
import ar.edu.uba.fi.jmit.monitorengine.Constants;
import ar.edu.uba.fi.jmit.monitorengine.ResourceProperties;
import ar.edu.uba.fi.jmit.monitorengine.core.AbstractRangeObserver;
import ar.edu.uba.fi.jmit.monitorengine.core.Monitor;
import ar.edu.uba.fi.jmit.monitorengine.core.monitor.MethodInvocationCounterMonitor;
import ar.edu.uba.fi.jmit.monitorengine.exception.MonitorEngineRuntimeException;
/**
 * Observer para tipos de monitoreo {@link MethodInvocationCounterMonitor}
 * @author Tomas
 *
 */
public class MethodInvocationCounterRangeObserver extends AbstractRangeObserver {

	@Override
	protected Event createEvent(Monitor monitor, Map<String, Serializable> data) {
		MethodInvocationCounterMonitor methodInvocationCounter = getMethodInvocationCounterMonitor(monitor);
		Event event = new Event();
		event.setData(data);
		event.addData(ResourceProperties.getProperty(Constants.RANGE_MONITORED_CLASS), methodInvocationCounter.getMonitoredClass());
		event.addData(ResourceProperties.getProperty(Constants.RANGE_MONITORED_METHOD), methodInvocationCounter.getMethodSignature());
		event.addData(ResourceProperties.getProperty(Constants.RANGE_MONITORED_OBJECT), methodInvocationCounter.getIdObject());
		event.setSource(this.getClass().getName());
		if(this.isAlert()) {
			event.setPriority(EventPriority.ALERT);
		} else {
			event.setPriority(EventPriority.NORMAL);
		}
		return event;
	}

	@Override
	protected Long getMonitorActualValue(Monitor monitor) {
		return getMethodInvocationCounterMonitor(monitor).getLastHourCount();
	}

	private MethodInvocationCounterMonitor getMethodInvocationCounterMonitor(Monitor monitor) {
		if(!(monitor instanceof MethodInvocationCounterMonitor)) {
			throw new MonitorEngineRuntimeException("A MethodInvocationCounterRangeObserver only observ a MethodInvocationCounterMonitor");
		}
		return (MethodInvocationCounterMonitor)monitor;
	}

}
